package com.example.videoserver.config;

import cn.hutool.core.io.FileUtil;
import com.example.videoserver.controller.FileUploadController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.concurrent.TimeUnit;

@Configuration
public class WebConfig implements WebMvcConfigurer {


    @Value("${upload.path}")
    private String uploadPath; // 在application.yml里定义上传文件的最终保存路径

    public static String resourcePath = "/static/**";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        File mkdir = FileUtil.mkdir(FileUploadController.uploadPath);
        // 将D盘下的static-files映射到/static路径下
        registry.addResourceHandler(resourcePath)
                .addResourceLocations("file:" + mkdir.getAbsolutePath() +"/")
                // 可以根据需要配置缓存策略和跨域策略
                .setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS).cachePublic());
    }
}